package com.ken.mqbase.topic;

import com.ken.constant.RabbitConstant;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;

/**
 * topic模式 生产者 （通配）
 * Created by renchongyu on 2018/11/12.
 */
public class ProductTopic {

    public static void main(String[] args) throws Exception{
        //1.创建一个ConnectionFactory
        ConnectionFactory conn = new ConnectionFactory();
        conn.setHost(RabbitConstant.RABBITMQ_IP);
        conn.setPort(RabbitConstant.RABBITMQ_PORT);
        conn.setUsername(RabbitConstant.RABBITMQ_USER_NAME);
        conn.setPassword(RabbitConstant.RABBITMQ_USER_PASSWORD);
        conn.setVirtualHost(RabbitConstant.RABBITMQ_VIRTUAL_HOST);

        //2.通过连接工厂连接
        Connection connection = conn.newConnection();

        //3.通过连接创建一个channel
        Channel channel = connection.createChannel();
        //4.声明
        String exchangeName = "test_topic_exchange";//路由
        String routingKey1 = "user.save";//路由键
        String routingKey2 = "user.update";//路由键
        String routingKey3 = "user.delete.abc";//路由键

        //5.发送
        String msg = "Hello world! My name is RabbitMQ.This is my topic exchange.";
        channel.basicPublish(exchangeName,routingKey1,null,msg.getBytes());
        channel.basicPublish(exchangeName,routingKey2,null,msg.getBytes());
        channel.basicPublish(exchangeName,routingKey3,null,msg.getBytes());

        //6.关闭连接
        channel.close();
        connection.close();

    }
}
